if not(datastream) then  
    require("datastream");  
end

concommand.Add("dm_loadout",function(ply,cmd,args)
	local class = args[1]
	if !class then LoadoutClass() end
	if class == "assassin" then LoadoutAssassin() end
	if class == "marksman" then LoadoutMarksman() end
	if class == "soldier" then LoadoutSoldier() end
	if class == "support" then LoadoutSupport() end
end)

datastream.Hook("Weapons",function(handler, id, encoded, decoded)
	Weapons = decoded
	print("\n\n***********************************************************\n\nReceived weapon list from server!\n\n")
	PrintTable(Weapons)
	print("\n\n***********************************************************\n\n")
end)

function LoadoutClass()
	local lcmenu = vgui.Create("DFrame")
	lcmenu:SetSize(500,75)
	lcmenu:Center()
	lcmenu:SetTitle("Class loadout menu")
	lcmenu:MakePopup()
	
	local assbut = vgui.Create("DButton",lcmenu)
	assbut:SetSize(105,35)
	assbut:SetPos(100-105/2,75/3*2-35/2)
	assbut:SetText("Assassin")
	assbut.DoClick = function()
		LoadoutAssassin()
		lcmenu:Close()
	end
	
	local marbut = vgui.Create("DButton",lcmenu)
	marbut:SetSize(105,35)
	marbut:SetPos(200-105/2,75/3*2-35/2)
	marbut:SetText("Marksman")
	marbut.DoClick = function()
		LoadoutMarksman()
		lcmenu:Close()
	end
	
	local solbut = vgui.Create("DButton",lcmenu)
	solbut:SetSize(105,35)
	solbut:SetPos(300-105/2,75/3*2-35/2)
	solbut:SetText("Soldier")
	solbut.DoClick = function()
		LoadoutSoldier()
		lcmenu:Close()
	end
	
	local supbut = vgui.Create("DButton",lcmenu)
	supbut:SetSize(105,35)
	supbut:SetPos(400-105/2,75/3*2-35/2)
	supbut:SetText("Support")
	supbut.DoClick = function()
		LoadoutSupport()
		lcmenu:Close()
	end
end

function WepName(wep)
	local name = wep
	name = string.Replace(name,"weapon_mdm_","")
	name = string.Replace(name,"_"," ")
	name = string.upper(name)
	return name
end

function LoadoutAssassin()
	lamenu = vgui.Create("DFrame")
	lamenu:SetSize(230,300)
	lamenu:Center()
	lamenu:SetTitle("Assassin loadout menu")
	lamenu:MakePopup()
	
	palist = vgui.Create("DComboBox",lamenu)
	palist:SetSize(100,225)
	palist:SetPos(10,35)
	palist:SetMultiple(false)
	palist:Clear()
	for i=1,LocalPlayer():GetNWInt("assassinlvl") do
		if Weapons.Primary.Assassin[i] then
			palist:AddItem(WepName(Weapons.Primary.Assassin[i]))
		end
	end

	salist = vgui.Create("DComboBox",lamenu)
	salist:SetSize(100,225)
	salist:SetPos(120,35)
	salist:SetMultiple(false)
	salist:Clear()
	for i=1,LocalPlayer():GetNWInt("assassinlvl") do
		if Weapons.Secondary.Assassin[i] then
			salist:AddItem(WepName(Weapons.Secondary.Assassin[i]))
		end
	end
	
	local a_apply = vgui.Create("DButton",lamenu)
	a_apply:SetSize(210,30)
	a_apply:SetPos(10,260)
	a_apply:SetText("Apply")
	a_apply.DoClick = function()
		local wep1 = "_"
		local wep2 = "_"
		if palist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Primary.Assassin) do
				if palist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep1 = v
				end
			end
		end
		if salist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Secondary.Assassin) do
				if salist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep2 = v
				end
			end
		end
		RunConsoleCommand("dm_req_loadout",wep1,wep2)
		lamenu:Close()
	end
end

function LoadoutSoldier()
	so_menu = vgui.Create("DFrame")
	so_menu:SetSize(230,300)
	so_menu:Center()
	so_menu:SetTitle("Soldier loadout menu")
	so_menu:MakePopup()
	
	psolist = vgui.Create("DComboBox",so_menu)
	psolist:SetSize(100,225)
	psolist:SetPos(10,35)
	psolist:SetMultiple(false)
	psolist:Clear()
	for i=1,LocalPlayer():GetNWInt("soldierlvl") do
		if Weapons.Primary.Soldier[i] then
			psolist:AddItem(WepName(Weapons.Primary.Soldier[i]))
		end
	end

	ssolist = vgui.Create("DComboBox",so_menu)
	ssolist:SetSize(100,225)
	ssolist:SetPos(120,35)
	ssolist:SetMultiple(false)
	ssolist:Clear()
	for i=1,LocalPlayer():GetNWInt("soldierlvl") do
		if Weapons.Secondary.Soldier[i] then
			ssolist:AddItem(WepName(Weapons.Secondary.Soldier[i]))
		end
	end
	
	local so_apply = vgui.Create("DButton",so_menu)
	so_apply:SetSize(210,30)
	so_apply:SetPos(10,260)
	so_apply:SetText("Apply")
	so_apply.DoClick = function()
		local wep1 = "_"
		local wep2 = "_"
		if psolist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Primary.Soldier) do
				if psolist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep1 = v
				end
			end
		end
		if ssolist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Secondary.Soldier) do
				if ssolist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep2 = v
				end
			end
		end
		RunConsoleCommand("dm_req_loadout",wep1,wep2)
		so_menu:Close()
	end
end

function LoadoutMarksman()
	lm_menu = vgui.Create("DFrame")
	lm_menu:SetSize(230,300)
	lm_menu:Center()
	lm_menu:SetTitle("Marksman loadout menu")
	lm_menu:MakePopup()
	
	pmlist = vgui.Create("DComboBox",lm_menu)
	pmlist:SetSize(100,225)
	pmlist:SetPos(10,35)
	pmlist:SetMultiple(false)
	pmlist:Clear()
	for i=1,LocalPlayer():GetNWInt("marksmanlvl") do
		if Weapons.Primary.Marksman[i] then
			pmlist:AddItem(WepName(Weapons.Primary.Marksman[i]))
		end
	end

	smlist = vgui.Create("DComboBox",lm_menu)
	smlist:SetSize(100,225)
	smlist:SetPos(120,35)
	smlist:SetMultiple(false)
	smlist:Clear()
	for i=1,LocalPlayer():GetNWInt("marksmanlvl") do
		if Weapons.Secondary.Marksman[i] then
			smlist:AddItem(WepName(Weapons.Secondary.Marksman[i]))
		end
	end
	
	local m_apply = vgui.Create("DButton",lm_menu)
	m_apply:SetSize(210,30)
	m_apply:SetPos(10,260)
	m_apply:SetText("Apply")
	m_apply.DoClick = function()
		local wep1 = "_"
		local wep2 = "_"
		if pmlist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Primary.Marksman) do
				if pmlist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep1 = v
				end
			end
		end
		if smlist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Secondary.Marksman) do
				if smlist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep2 = v
				end
			end
		end
		RunConsoleCommand("dm_req_loadout",wep1,wep2)
		lm_menu:Close()
	end
end

function LoadoutSupport()
	su_menu = vgui.Create("DFrame")
	su_menu:SetSize(230,300)
	su_menu:Center()
	su_menu:SetTitle("Support loadout menu")
	su_menu:MakePopup()
	
	psulist = vgui.Create("DComboBox",su_menu)
	psulist:SetSize(100,225)
	psulist:SetPos(10,35)
	psulist:SetMultiple(false)
	psulist:Clear()
	for i=1,LocalPlayer():GetNWInt("supportlvl") do
		if Weapons.Primary.Support[i] then
			psulist:AddItem(WepName(Weapons.Primary.Support[i]))
		end
	end

	ssulist = vgui.Create("DComboBox",su_menu)
	ssulist:SetSize(100,225)
	ssulist:SetPos(120,35)
	ssulist:SetMultiple(false)
	ssulist:Clear()
	for i=1,LocalPlayer():GetNWInt("supportlvl") do
		if Weapons.Secondary.Support[i] then
			ssulist:AddItem(WepName(Weapons.Secondary.Support[i]))
		end
	end
	
	local su_apply = vgui.Create("DButton",su_menu)
	su_apply:SetSize(210,30)
	su_apply:SetPos(10,260)
	su_apply:SetText("Apply")
	su_apply.DoClick = function()
		local wep1 = "_"
		local wep2 = "_"
		if psulist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Primary.Support) do
				if psulist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep1 = v
				end
			end
		end
		if ssulist:GetSelectedItems()[1] then
			for k,v in pairs(Weapons.Secondary.Support) do
				if ssulist:GetSelectedItems()[1]:GetValue() == WepName(v) then
					wep2 = v
				end
			end
		end
		RunConsoleCommand("dm_req_loadout",wep1,wep2)
		su_menu:Close()
	end
end